import 'dart:convert';

import '../core.dart';
import '../model/log.dart';
import '../model/options.dart';
import 'index.dart';

/// 普通日志最终的上报管道
NormalLogPipe createReportPipe(Core aegis) {
  return (List<NormalLog> msgs, Resolve resolve) {
    aegis.send(
      options: Options(
        url: aegis.config.url,
        type: SendType.log,
        data: buildLogParams(msgs),
        method: Method.POST,
        contentType: 'application/x-www-form-urlencoded',
      ),
    );
  };
}

String buildLogParams(List<NormalLog> logs) {
  List<String> levels = [];
  List<String> msgs = [];
  for (int i = 0; i < logs.length; i++) {
    levels.add(logs[i].level);
    msgs.add(logs[i].msg);
  }

  return jsonEncode({'count': logs.length, 'msg': msgs, 'level': levels});
}
